package ru.sushka.lunch.dao.files;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import ru.sushka.lunch.enitiy.MenuItem;

/**
 * 
 * csv формат: course.id, date
 */
public class FileMenuItemDao extends FileDao<MenuItem> {

    final static String FILE_NAME = "./db/menu.csv";

    protected MenuItem parseCsv(String csvline) throws ParseException {
        // validating
        if (csvline.trim().length() == 0) {
            throw new ParseException(csvline, 0);
        }
        String[] args = csvline.split(";");
        if (args.length != 2) {
            throw new ParseException(csvline, 0);
        }
        int courseId = Integer.parseInt(args[0].trim());

        Calendar date = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        date.setTime(sdf.parse(args[1].trim()));

        return new MenuItem(courseId, date);

    }

    protected String convertToCsv(MenuItem menuItem) {
        int courseId = menuItem.getCourseId();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String date = sdf.format(menuItem.getDate().getTime());
        return courseId + ";" + date;
    }

    @Override
    protected String getFileName() {
        return FILE_NAME;
    }

}
